//
//  LTQPictureView.swift
//  WeiBoProject
//
//  Created by MAC on 16/3/2.
//  Copyright © 2016年 LTQ. All rights reserved.
//

import UIKit
//定义重用标识
let LTQPictureViewCollectionViewReuseIdentifier = "LTQPictureViewCollectionViewReuseIdentifier"
//定义一个间距
let ItemMargin: CGFloat = 10
let ItemWH = (kUIScreenWidth - 4 * ItemMargin - 1)/3
class LTQPictureView: UICollectionView,UICollectionViewDataSource {
    
    
    var pictureList:[LTQPictureViewModel]?{
        didSet{
            self.snp_updateConstraints { (make) -> Void in
                let size = self.calPictureSize()
                make.width.equalTo(size.width)
                make.height.equalTo(size.height)
                
            }
            reloadData()
        }
    }
    
    
    
    
    //传递数据
    // 刚才我把lazy去掉,就没有报错了?
    var flowLayout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        //设置流水布局
        super.init(frame: frame, collectionViewLayout:flowLayout)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //
    private func setupUI(){
        self.dataSource = self
        
        backgroundColor = UIColor.whiteColor()

        
        self.registerClass(LTQPictureViewControllerCell.self, forCellWithReuseIdentifier: LTQPictureViewCollectionViewReuseIdentifier)
        //设置水平
        flowLayout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        //设置cell的大小
        flowLayout.itemSize = CGSizeMake(ItemWH, ItemWH)
        flowLayout.minimumInteritemSpacing = ItemMargin
        flowLayout.minimumLineSpacing = ItemMargin

    }
    
    private func calPictureSize()->CGSize{
        let count = pictureList?.count
//        pictureViewModel?.statusModel?.pic_urls?.count
        switch count ?? 0{
            /*
            图片1张的时候,我们可以返回一个item宽高,也可以返回 宽 = itemW * 3 + 2 * itemMargin ,高 itemH
            2,3张图片 宽 = itemW * 3 + 2 * itemMargin ,高 itemH
            */
        case 1,2,3: return CGSizeMake(ItemWH * 3 + 2 * ItemMargin,ItemWH)
//            4张图片, 宽 = itmeW * 2 + itemMargin 高 = 宽
        case 4: return CGSizeMake(ItemWH * 2 + ItemMargin , ItemWH * 2)
//             宽 = itemW * 3 + 2 * itemMargin ,高 itemH*2 + itemMargin
        case 5,6,7,8,9:
            let row = CGFloat((count!-1)/3 + 1)
            return CGSizeMake(kUIScreenWidth - 2 * ItemMargin,ItemWH * row + ItemMargin * row )
        default: return CGSizeZero
            
        }
        
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pictureList?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(LTQPictureViewCollectionViewReuseIdentifier, forIndexPath: indexPath) as! LTQPictureViewControllerCell
        

        cell.pictureViewModel = pictureList![indexPath.item]
        
        return cell
    }
    
    
}

//相当于自定义cell
class LTQPictureViewControllerCell: UICollectionViewCell{
    
    var pictureViewModel: LTQPictureViewModel?{
        didSet{
            
            let url = NSURL(string: pictureViewModel?.model?.thumbnail_pic ?? "")
            
            if let Ourl = url{
                iconImageView.sd_setImageWithURL(Ourl)
                
            }
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func setupUI(){
        
        addSubview(iconImageView)
        iconImageView.frame = self.bounds
    }

    private lazy var iconImageView: UIImageView = {
        let img = UIImageView()
        return img
        }()

}



